package com.solo.servlet;

import com.solo.entity.User;
import com.solo.service.UserService;
import com.solo.service.impl.UserServiceImpl;
import com.solo.utils.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
    private UserService userService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取对应的请求参数
        String action = request.getParameter("method");
        System.out.println("action===" + action);
        try {
            switch (action) {
                case "new":
                    showNewForm(request, response);
                    break;
                case "add":
                    addUser(request, response);
                    break;
                case "delete":
                    deleteUser(request, response);
                    break;
                case "edit":
                    showEditForm(request, response);
                    break;
                case "update":
                    updateUser(request, response);
                    break;
                case "search":
                    search(request, response);
                    break;
                default:
                    listUser(request, response);
                    break;
            }
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/addUser.jsp");
        dispatcher.forward(request, response);
    }


    private void addUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String phonenumber = request.getParameter("phonenumber");

        System.out.println("username===" + username);
        System.out.println("password===" + password);
        User user = new User(username, password,name,email,phonenumber,gender);
        userService = new UserServiceImpl();
        System.out.println("userService===" + userService);
        userService.addUser(user);
        System.out.println("user===" + user);
        listUser(request, response);
    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));

        User user = new User(id);
        userService = new UserServiceImpl();
        userService.deleteUser(user);
        listUser(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        // String[] genders = new String[]{"0", "1"};

        ArrayList<String> genderList = new ArrayList<String>();

        genderList.add("0");
        genderList.add("1");

       request.setAttribute("genderList", genderList);

       //  for(int i = 0;i < genderList.size();i++){
       //      System.out.println(genderList.get(i));
       // }

        for (String i : genderList) {
            System.out.println("字符串=="+i);
        }


        userService = new UserServiceImpl();
        User user = userService.selectUserById(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/editUser.jsp");
        request.setAttribute("user", user);
        request.setAttribute("genderList", genderList);
        dispatcher.forward(request, response);
    }
    private void updateUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String phonenumber = request.getParameter("phonenumber");

        User user = new User(id,username, password,name,email,phonenumber,gender);

        userService = new UserServiceImpl();
        userService.updateUser(user);
        listUser(request, response);
    }

    private void listUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        userService = new UserServiceImpl();
        List<User> userList = userService.selectAllUser();
        request.setAttribute("userList", userList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/user.jsp");
        dispatcher.forward(request, response);
    }

    private void search(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        System.out.println("username===" + username);
        userService = new UserServiceImpl();
        List<User> userList = userService.selectUserByUsername(username);
        request.setAttribute("userList", userList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/user.jsp");
        dispatcher.forward(request, response);
    }
}